using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Ent.Web.Helper;
using Ent.Web.WebControl;
namespace Ent.Web.Helper
{
    public enum MediaType { Audio, Video, Image, None }
    public struct MediaFile
    {
        public MediaType MediaType;
        public string Type;
        public string Codebase;
    }
    public class MediaHelper
    {
        private static Dictionary<string, MediaFile> _media;
        public static Dictionary<string, MediaFile> Media
        {
            get {
                if (_media == null)
                    Ent.Web.Helper.MediaHelper.Init(ConfigurationManager.AppSettings.Get("Media"));
                return _media;
            }
            set
            {
                _media = value;
            }
        }
        public static void Init(string path)
        {
            path = HttpRuntime.AppDomainAppPath + "/" + path;
            if (_media == null)
                _media = new Dictionary<string, MediaFile>();
            XMLHelper xml = new XMLHelper(path);
            string[] exts = xml.GetAttributesOfAllNodes("//Media/File", "ext");
            for (int i = 0; i < exts.Length; i++)
            {
                string ext = exts.GetValue(i).ToString();
                MediaFile mf = new MediaFile();
                string baseType = xml.GetAttributeForSingleNode("//Media/File[@ext='" + ext + "']", "base");
                switch (baseType.ToLower())
                { 
                    case "audio":
                        mf.MediaType = MediaType.Audio;
                        break;
                    case "video":
                        mf.MediaType = MediaType.Video;
                        break;
                    case "Image":
                        mf.MediaType = MediaType.Image;
                        break;
                    default:
                        mf.MediaType = MediaType.None;
                        break;
                }
                mf.Type = xml.GetInnerText("//Media/File[@ext='" + ext + "']/Type");
                mf.Codebase = xml.GetInnerText("//Media/File[@ext='" + ext + "']/Codebase");
                _media.Add(ext, mf);
            }
        }
        public static string GetEmbedMedia(MediaPlayer mp, string id, string ext)
        {
            string content = "";
            content += "<embed type=\"" + MediaHelper.GetFileType(ext) + "\" ";
            content += "pluginspage=\"" + MediaHelper.GetCodeBase(ext) + "\" ";
            content += "name=\"" + id + "\" ";
            content += "ShowStatusBar=\"" + BrowserHelper.GetBrowserBool(false) + "\" ";
            content += "EnableContextMenu=\"" + BrowserHelper.GetBrowserBool(true) + "\" ";
            content += "ShowControls=\"" + BrowserHelper.GetBrowserBool(mp.ShowControls) + "\" ";
            content += "AutoStart=\"" + BrowserHelper.GetBrowserBool(mp.AutoStart) + "\" ";
            content += "width=\"" + mp.Width + "\" ";
            content += "height=\"" + mp.Height + "\" ";
            content += "loop=\"" + BrowserHelper.GetBrowserBool(false) + "\" ";
            content += "src=\"" + mp.FileName + "\" />";
            return content;
        }
        public static string GetObjectMedia(MediaPlayer mp)
        {
            string content = "";
            int stIndex = mp.FileName.LastIndexOf('/') + 1;
            int endIndex = mp.FileName.LastIndexOf('.') == -1 ? mp.FileName.Length - 1 : mp.FileName.LastIndexOf('.');
            string id = mp.FileName.Substring(stIndex, endIndex - stIndex);
            string ext = mp.FileName.Substring(mp.FileName.LastIndexOf('.') + 1);
            //Depricated: 22D6F312-B0F6-11D0-94AB-0080C74C7E95
            //Updated: 6BF52A52-394A-11d3-B153-00C04F79FAA6
            //content += "<script type=\"text/javascript\">alert(document.getElementById('" + id + "'));</script>";
            content += "<OBJECT id=\"" + id + "\" CLASSID=\"CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6\" ";
            content += "codebase=\"" + MediaHelper.GetCodeBase(ext) + "\" ";
            content += "type=\"" + MediaHelper.GetFileType(ext) + "\" width=\"" + mp.Width + "\" height=\"" + mp.Height + "\">";
            content += "<PARAM NAME=\"URL\" VALUE=\"" + mp.FileName + "\">";
            content += "<PARAM NAME=\"enabled\" VALUE=\"" + BrowserHelper.GetBrowserBool(true) + "\">";
            content += "<PARAM NAME=\"AutoStart\" VALUE=\"" + BrowserHelper.GetBrowserBool(mp.AutoStart) + "\">";
            content += "<PARAM NAME=\"showcontrols\" value=\"" + BrowserHelper.GetBrowserBool(mp.ShowControls) + "\">";
            content += "<PARAM NAME=\"playCount\" value=\"1\">";
            content += "<PARAM NAME=\"Volume\" value=\"100\">";
            content += "<PARAM NAME=\"balance\" VALUE=\"0\">";
            content += "<PARAM NAME=\"Rate\" VALUE=\"1.0\">";
            content += "<PARAM name='StretchToFit' value='" + BrowserHelper.GetBrowserBool(true) + "'>";
            content += "<PARAM NAME=\"Mute\" VALUE=\"" + BrowserHelper.GetBrowserBool(false) + "\">";
            content += "<PARAM NAME=\"fullScreen\" VALUE=\"" + BrowserHelper.GetBrowserBool(false) + "\">";
            string uimode = mp.ShowControls ? "full" : "none";
            content += "<PARAM NAME=\"uiMode\" value=\"" + uimode + "\">";
            content += GetEmbedMedia(mp, id, ext);
            content += "</OBJECT>";
            return content;
        }
        private static string GetFileType(string extension)
        {
            if (Media.ContainsKey(extension))
                return Media[extension].Type;
            else if (Media.ContainsKey("*"))
                return Media["*"].Type;
            else
                return "";
        }
        private static string GetCodeBase(string extension)
        {
            if (Media.ContainsKey(extension))
                return Media[extension].Codebase;
            else if (Media.ContainsKey("*"))
                return Media["*"].Codebase;
            else
                return "";
        }
        public static bool IsMediaTypeSupported(string extension, MediaType fType)
        {
            if (Media.ContainsKey(extension))
                return (Media[extension].MediaType == fType);
            return false;
        }
    }
}